package com.js.data.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.js.data.common.constant.SymbolConstant;
import com.js.data.system.entity.SysDept;
import com.js.data.system.entity.SysUserDept;
import com.js.data.system.mapper.SysUserDeptMapper;
import com.js.data.system.service.DeptService;
import com.js.data.system.service.UserDeptService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户部门关联 服务实现类
 * </p>
 *
 * @author wujj
 * @since 2022-09-06
 */
@Service
public class UserDeptServiceImpl extends ServiceImpl<SysUserDeptMapper, SysUserDept> implements UserDeptService {


    @Resource
    private DeptService deptService;

    /**
     * 用户部门
     *
     * @param deptIds 用户部门
     * @param userId  用户uid
     */
    @Override
    public void saveBatchToDB(List<Long> deptIds, Long userId) {
        if (CollectionUtils.isNotEmpty(deptIds)) {
            List<SysUserDept> userDeptList = new ArrayList<>();
            deptIds.forEach(deptId -> {
                SysUserDept ud = new SysUserDept();
                ud.setUserId(userId);
                ud.setDeptId(deptId);
                userDeptList.add(ud);
            });
            saveBatch(userDeptList);
        }
    }

    /**
     * 根据用户uid 删除
     *
     * @param userId 用户uid
     */
    @Override
    public void removeByUserId(Long userId) {
        lambdaUpdate().eq(SysUserDept::getUserId, userId).remove();
    }

    /**
     * 批量删除用户
     *
     * @param userIds 用户ids
     */
    @Override
    public void removeByUserIds(List<Long> userIds) {
        lambdaUpdate().in(SysUserDept::getUserId, userIds).remove();
    }

    /**
     * 获取用户 指定的部门Id
     *
     * @param userId 用户uid
     * @return List<Integer>
     */
    @Override
    public List<Long> getAssignDeptIds(Long userId) {
        return lambdaQuery().eq(SysUserDept::getUserId, userId).list().stream().map(SysUserDept::getDeptId).collect(Collectors.toList());
    }

    /**
     * 根据部门id获取用户id
     *
     * @param deptIds 部门id
     * @return 用户id
     */
    @Override
    public List<Long> getUserIdByDeptIds(List<Long> deptIds) {
        return lambdaQuery().in(SysUserDept::getDeptId, deptIds)
                .list().stream().map(SysUserDept::getUserId).collect(Collectors.toList());
    }

    /**
     * 获取用户的部门数据权限
     *
     * @param userId         用户uid
     * @param userSelfDeptId 用户自己部门id
     * @param permType       用户数据权限  0-全部 1-本人 2-本部门 3-本部门及下级部门 4-指定
     */
    @Override
    public void getUserPermDeptIds(Long userId, Long userSelfDeptId, String permType,
                                   List<Long> dataScopeIds) {
        // 网约房
        SysDept sysDept = deptService.getById(userSelfDeptId);
        if (SymbolConstant.TOW_STR.equals(sysDept.getOrgType())) {
            dataScopeIds.add(sysDept.getId());
            return;
        }
        switch (permType) {
            case SymbolConstant.ZERO_STR:
                dataScopeIds.addAll(deptService.listAllDept());
                break;
            case SymbolConstant.ONE_STR:
                break;
            case SymbolConstant.TOW_STR:
                dataScopeIds.add(userSelfDeptId);
                break;
            case SymbolConstant.THREE_STR:
                dataScopeIds.addAll(deptService.listAllChildDeptIdAndSelf(userSelfDeptId));
                break;
            case SymbolConstant.FOUR_STR:
                dataScopeIds.addAll(getAssignDeptIds(userId));
                break;
        }
    }
}
